package com.itboye.core.utils;

import java.util.ArrayList;

import com.itboye.config.GlobalConfig;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;

/***
 * 
 * @author Mr.w 2016/7/21 17:35 网络图片的等比例缩放
 */
public class MyRunnable {

	ArrayList<String> url;
	ImageView imageView;
	Context context;
	int size;
	int screenWidth;
	DisplayMetrics displayMetrics;
	LinearLayout image_container;
	int position;
	ProgressDialog progressDialog;

	public MyRunnable(Context context, ArrayList<String> url,
			ImageView imageView, int size, int screenWidth,
			DisplayMetrics displayMetrics, LinearLayout image_container) {
		this.url = url;
		this.imageView = imageView;
		this.size = size;
		this.context = context;
		this.screenWidth = screenWidth;
		this.displayMetrics = displayMetrics;
		this.image_container = image_container;
		new Thread(runnable).start();
		progressDialog = new ProgressDialog(context);
		progressDialog.setTitle(null);
		progressDialog.setMessage("正在加载中........");
		progressDialog.setCancelable(true);
		progressDialog.show();
	}

	// 获取网络图片，转换成对应的bitmap
	Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-d method stub
			Bitmap bitmap = ImageLoader.getInstance().loadImageSync(
					GlobalConfig.API_URL + "/picture/index?id="
							+ url.get(position));

			if (bitmap != null) {
				Message msg = new Message();
				msg.obj = bitmap;
				handler.sendMessage(msg);
			}
		}
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			// if (model.getImg().size() != 0) {
			// for (int i = 0; i < model.getImg().size(); i++) {
			ImageView imageView = new ImageView(context);
			LayoutParams layoutParams = new LayoutParams(screenWidth,
					LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(0, 4, 2, 0);
			imageView.setLayoutParams(layoutParams);
			setImageViewMathParent((Bitmap) msg.obj, imageView);

			// XImageLoader.load(GlobalConfig.API_URL
			// + "/picture/index?id="
			// + model.getImg().get(i), imageView);
			image_container.addView(imageView);
			// }
			// } else {
			// image_container.setVisibility(View.GONE);
			// }

			Log.e("position", position + "a");
			Log.e("size", size + "a");
			if (position < size - 1) {
				handler.post(runnable);
				position++;
			} else {

				handler.removeCallbacks(runnable);

			}

		};
	};

	public void setImageViewMathParent(Bitmap bitmap, ImageView image) {
		if (bitmap != null) {
			float scalew = (float) displayMetrics.widthPixels
					/ (float) bitmap.getWidth();
			image.setScaleType(ScaleType.MATRIX);
			Matrix matrix = new Matrix();
			image.setAdjustViewBounds(true);
			if (displayMetrics.widthPixels < bitmap.getWidth()) {
				matrix.postScale(scalew, scalew);
			} else {
				matrix.postScale(1 / scalew, 1 / scalew);
			}
			image.setMaxWidth(displayMetrics.widthPixels);
			float ss = displayMetrics.heightPixels > bitmap.getHeight() ? displayMetrics.heightPixels
					: bitmap.getHeight();
			image.setMaxWidth((int) ss);
			image.setImageBitmap(bitmap);
			if (bitmap != null && bitmap.isRecycled()) {
				bitmap.recycle();
			}
			if (position == size - 1) {
				progressDialog.dismiss();
			}
		}

	}
}
